package com.sxbbc.plat.service;

import java.util.Date;
import java.util.List;

import com.sxbbc.common.core.service.*;
import com.sxbbc.common.core.service.system.mode.ModeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.basics.utils.calculation.CalcuUtils;
import com.sxbbc.common.basics.utils.date.DateUtil;
import com.sxbbc.common.basics.utils.statics.StaticUtils;
import com.sxbbc.common.core.entity.jz.InfoShopping;
import com.sxbbc.common.core.entity.jz.OrderConfigGroup;
import com.sxbbc.common.core.entity.jz.OrderDetail;
import com.sxbbc.common.core.entity.jz.OrderMain;
import com.sxbbc.common.core.entity.jz.OrderRefundLog;
import com.sxbbc.common.core.enums.GroupStatusEnums;
import com.sxbbc.common.core.enums.OrderStatusEnums;
import com.sxbbc.common.core.mapper.jz.OrderMainMapper;
import com.sxbbc.common.core.service.system.redis.listener.OrderListerService;

/**
 * 订单定时器处理类
 * 
 * @author xiaoky
 * @date 2019-07-14
 */
@Service
public class OrderTaskService {
	@Autowired
	private IAccountChangeService accountChangeService;
	@Autowired
	private IOrderMainService orderMainService;
	@Autowired
	private IInfoShoppingService infoShoppingService;
	@Autowired
	private OrderMainMapper orderMainMapper;
	@Autowired
	private IOrderConfigGroupService orderConfigGroupService;
	@Autowired
	private IOrderRefundLogService orderRefundLogService;
	@Autowired
	private IOrderDetailService orderDetailService;
	@Autowired
	private OrderListerService orderListerService;
	@Autowired
	private ModeService modeService;


	/**
	 * 定时取消过期订单
	 * 
	 * @author xiaoky
	 */
	public void autoCancelOrder() {
		QueryWrapper<OrderMain> wrapper = new QueryWrapper<OrderMain>();
		wrapper.eq("order_status", OrderStatusEnums.COMFIRE.getId()).lt("payend_time", new Date());
		List<OrderMain> orderList = orderMainService.list(wrapper);
		for (OrderMain order : orderList) {
			orderListerService.handleOrderTimeout(order.getOrderCode());
		}
	}

	/**
	 * 定时处理收货
	 * 
	 * @author xiaoky
	 */
	public void autoOrderReceive() {
		InfoShopping infoShop = infoShoppingService.getOne(new QueryWrapper<InfoShopping>());
		// 判断是否需要自动收货
		if (!CalcuUtils.getInstance().isEquals(StaticUtils.NO_AUTO_OPERATIO, infoShop.getAutoReceived())) {
			QueryWrapper<OrderMain> wrapper = new QueryWrapper<OrderMain>();
			Date receiveTime = DateUtil.getInstance().diffDate(new Date(), infoShop.getAutoReceived());
			wrapper.eq("order_status", OrderStatusEnums.HAS_SEND.getId()).lt("send_time", receiveTime);
			List<OrderMain> orderList = orderMainService.list(wrapper);
			for (OrderMain order : orderList) {
				order.setReceivedTime(new Date());
				order.setOrderStatus(OrderStatusEnums.RECEIVED.getId());
				orderMainService.updateById(order);
			}
		}
	}

	/**
	 * 定时交易完成订单
	 * @author xiaoky
	 */
	public void autoOrderOver() {
		InfoShopping infoShop = infoShoppingService.getOne(new QueryWrapper<InfoShopping>());
		// 判断是否需要自动交易完成
		if (!CalcuUtils.getInstance().isEquals(StaticUtils.NO_AUTO_OPERATIO, infoShop.getAutoOver())) {
			List<OrderMain> orderList = orderMainMapper.queryOverOrder(infoShop.getAutoOver());
			for (OrderMain order : orderList) {
				order.setOrderStatus(OrderStatusEnums.OVER.getId());
				order.setOverTime(new Date());
				orderMainService.updateById(order);
				//处理 解冻商家、代理对应的余额//处理 解冻商家、代理对应的余额
//				accountChangeService.handleFreezeCustomerMoney(new FreezeDTO(FreezeStatusEnums.SEND.getId(), order.getPayCode(), 1));
//				accountChangeService.handleFreezeBusinessMoney(new FreezeDTO(FreezeStatusEnums.SEND.getId(),order.getPayCode(),1));
//				accountChangeService.handleFreezeAgentMoney(new FreezeDTO(FreezeStatusEnums.SEND.getId(),order.getPayCode(),1));
			}
		}
	}

	/**
	 * 定时处理未完成团购订单
	 * @author xiaoky
	 */
	public void autoUnfinishGroup() {
		QueryWrapper<OrderConfigGroup> wrapper = new QueryWrapper<OrderConfigGroup>();
		wrapper.eq("status", GroupStatusEnums.GROUP_ING.getId()).lt("over_time", new Date());
		List<OrderConfigGroup> groupList = orderConfigGroupService.list(wrapper);
		for (OrderConfigGroup group:groupList) {
			handleUnfinishGroup(group);
		}
	}
	
	/**
	 * 处理团购订单
	 * @param group
	 * @author xiaoky
	 */
	public void handleUnfinishGroup(OrderConfigGroup group) {
		// 团购状态改为已退款
		group.setStatus(GroupStatusEnums.GROUP_REFUND.getId());
		group.setRefund(StaticUtils.STATUS_YES);
		orderConfigGroupService.updateById(group);
		// 将对应的订单退款
		List<OrderDetail> detailList = orderDetailService.listByOrderCode(group.getOrderCode());
		for (OrderDetail detail:detailList) {
			orderRefundLogService.handleDirectRefund(group.getCid(), detail.getId(), "");
		}
	}

	/**
	 * 定时处理订单退款
	 * @author xiaoky
	 */
	public void autoOrderRefund() {
		InfoShopping infoShop = infoShoppingService.getOne(new QueryWrapper<InfoShopping>());
		// 判断是否需要自动退款
		if (!CalcuUtils.getInstance().isEquals(StaticUtils.NO_AUTO_OPERATIO, infoShop.getAutoRefund())) {
			Date refundDate = DateUtil.getInstance().diffDate(new Date(), infoShop.getAutoRefund());
			QueryWrapper<OrderRefundLog> wrapper = new QueryWrapper<OrderRefundLog>();
			wrapper.eq("status", StaticUtils.STATUS_APPLY).lt("create_time", refundDate);
			List<OrderRefundLog> refundList = orderRefundLogService.list(wrapper);
			for (OrderRefundLog refund : refundList) {
				// 修改退款状态
				refund.setCheckTime(new Date());
				refund.setStatus(StaticUtils.STATUS_SUCCESS);
				orderRefundLogService.updateById(refund);
				orderRefundLogService.handleDirectRefund(refund.getCid(), refund.getOrderDetailId(), "");
			}
		}
	}

}
